/*
 * GoogleDocumentsTableModel.java
 */

package pl.edu.pw.mini.gui;

import com.google.gdata.data.DateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.table.AbstractTableModel;
import pl.edu.pw.mini.adapter.documents.Document;
import pl.edu.pw.mini.adapter.documents.GoogleDocument;

/**
 *
 * @author cdendek
 */
public class GoogleDocumentsTableModel extends AbstractTableModel {
    
    protected List<Document> documentList;
    
    protected abstract class ColumnDefinition{
        public abstract String getName();
        public abstract Object getValue(GoogleDocument gdoc);
    }
    
    protected Map<Integer,ColumnDefinition> definitions = new HashMap<Integer,ColumnDefinition>();
    
    {
        int i=0;
        definitions.put(Integer.valueOf(i++), new ColumnDefinition(){
            public String getName() {
                return "Title";
            }

            public Object getValue(GoogleDocument gdoc) {
                return gdoc.getTitle();
            }           
        });
        definitions.put(Integer.valueOf(i++), new ColumnDefinition(){
            public String getName() {
                return "Version";
            }

            public Object getValue(GoogleDocument gdoc) {
                return gdoc.getVersion();
            }           
        });
        definitions.put(Integer.valueOf(i++), new ColumnDefinition(){
            public String getName() {
                return "Last edited at";
            }

            public Object getValue(GoogleDocument gdoc) {
                DateTime dt=gdoc.getLastEdited();
                return dt==null?"":dt.toUiString();
            }           
        });
        definitions.put(Integer.valueOf(i++), new ColumnDefinition(){
            public String getName() {
                return "Last updated at";
            }

            public Object getValue(GoogleDocument gdoc) {
                DateTime dt=gdoc.getUpdated();
                return dt==null?"":dt.toUiString();
            }           
        });
    }
    
    
    /** Creates a new instance of GoogleDocumentsTableModel */
    public GoogleDocumentsTableModel(List<Document> docList) {
        documentList=docList;
    }

    public int getRowCount() {
        return documentList==null?0:documentList.size();
    }

    public int getColumnCount() {
        return definitions.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        GoogleDocument gdoc=(GoogleDocument)documentList.get(rowIndex);
        ColumnDefinition def=definitions.get(Integer.valueOf(columnIndex));
        if (def==null){
            return null;
        }
        Object retObject=def.getValue(gdoc);
        return retObject==null?"":retObject;
    }
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public String getColumnName(int column) {
        ColumnDefinition def=definitions.get(Integer.valueOf(column));
        if (def==null){
            return "";
        }
        return def.getName();
    }
    
    public Document getAt(int index){
        return documentList.get(index);
    }
}
